package org.elec.mac.ehcache;

import org.elec.mac.constants.Constants;
import org.elec.mac.entity.*;
import org.elec.mac.service.*;
import org.elec.mac.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Bean on 2017/5/11.
 */
@Component
public class ManageEhCache {

    @Autowired
    private CacheConfiguration cacheConfiguration;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysActionService sysActionService;

    @Autowired
    private SysRoleUserService sysRoleUserService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private SysRoleActionService sysRoleActionService;

    public void cacheInit() {
        //
        EhCacheCacheManager cacheManager = cacheConfiguration
                .ehCacheCacheManager(cacheConfiguration.ehCacheManagerFactoryBean());
        //
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isValid", Constants.IS_VALID_TRUE);
        // 用户
        List<SysUser> userList = sysUserService.list(param);
        initUserCache(userList, cacheManager);
        // 菜单
        List<SysMenu> menuList = sysMenuService.list(null);
        initMenuCache(menuList, cacheManager);
        // 角色
        List<SysRole> roleList = sysRoleService.list(null);
        initRoleCache(roleList, cacheManager);
        // 功能
        List<SysAction> actionList = sysActionService.list(null);
        // 用户-角色
        List<SysRoleUser> roleUserList = sysRoleUserService.list(null);
        initRoleUser(roleUserList, cacheManager);
        // 角色-菜单
        List<SysRoleMenu> roleMenuList = sysRoleMenuService.list(null);
        // 角色-功能
        List<SysRoleAction> roleActionList = sysRoleActionService.list(null);
        //
        initRoleMenu(roleMenuList, menuList, roleList, cacheManager);
        //
        initRoleAction(roleActionList, actionList, roleList, cacheManager);
    }

    /**
     * 用户缓存
     * @param userList
     */
    private void initUserCache(List<SysUser> userList, EhCacheCacheManager cacheManager) {
        Cache cache = cacheManager.getCache("userCache");
        for(SysUser user : userList) {
            cache.put(user.getUsername(), user);
        }
    }

    /**
     *
     * @param roleList
     */
    private void initRoleCache(List<SysRole> roleList, EhCacheCacheManager cacheManager) {
        Cache cache = cacheManager.getCache("roleCache");
        cache.put("roleCache", roleList);
    }

    /**
     *
     * @param menuList
     * @param cacheManager
     */
    private void initMenuCache(List<SysMenu> menuList, EhCacheCacheManager cacheManager) {
        Cache cache = cacheManager.getCache("menuCache");
        cache.put("menuCache", menuList);
    }

    /**
     * 用户-角色
     */
    private void initRoleUser(List<SysRoleUser> roleUserList, EhCacheCacheManager cacheManager) {
        Cache cache = cacheManager.getCache("roleUserCache");
        for(SysRoleUser roleUser : roleUserList) {
            cache.put(roleUser.getUserId(), roleUser.getRoleId());
        }
    }

    /**
     * 角色-菜单
     */
    private void initRoleMenu(List<SysRoleMenu> roleMenuList, List<SysMenu> menuList,
                              List<SysRole> roleList, EhCacheCacheManager cacheManager) {
        Cache cache = cacheManager.getCache("roleMenuCache");
        // 角色菜单集合匹配
        for(SysRole sysRole : roleList) {
            Long roleId = sysRole.getId();
            List<SysMenu> sysMenus = new ArrayList<>();
            for(SysRoleMenu sysRoleMenu : roleMenuList) {
                if(sysRoleMenu.getRoleId().equals(roleId)) {
                    for(SysMenu sysMenu : menuList) {
                        if(sysMenu.getId().equals(sysRoleMenu.getMenuId())) {
                            sysMenus.add(sysMenu);
                        }
                    }
                }
            }
            // 菜单排序
            this.orderMenuList(sysMenus);
            cache.put(roleId, sysMenus);
        }
    }

    /**
     * 角色-功能
     */
    private void initRoleAction(List<SysRoleAction> sysRoleActionList, List<SysAction> actionList,
                                List<SysRole> roleList, EhCacheCacheManager cacheManager) {
        Cache cache = cacheManager.getCache("roleActionCache");
        // 角色菜单集合匹配
        for(SysRole sysRole : roleList) {
            Long roleId = sysRole.getId();
            List<SysAction> sysActions = new ArrayList<>();
            for(SysRoleAction sysRoleAction : sysRoleActionList) {
                if(sysRoleAction.getRoleId().equals(roleId)) {
                    for(SysAction sysAction : actionList) {
                        if(sysAction.getActionCode().equals(sysRoleAction.getActionCode())) {
                            sysActions.add(sysAction);
                        }
                    }
                }
            }
            cache.put(roleId, sysActions);
        }
    }

    /**
     * 菜单排序
     * @param menuList
     */
    private void orderMenuList(List<SysMenu> menuList) {
        if (menuList != null && menuList.size() > 0) {
            int total = menuList.size();
            for (int i = 0; i < total; i++) {
                for (int j = 0; j < total - (i + 1); j++) {
                    SysMenu menu1 = menuList.get(j);
                    SysMenu menu2 = menuList.get(j + 1);
                    int order1 = Utils.getIntValue(menu1.getShowOrder());
                    int order2 = Utils.getIntValue(menu2.getShowOrder());
                    if (order1 > order2) {
                        menuList.set(j, menu2);
                        menuList.set(j + 1, menu1);
                    }
                }
            }
        }
    }

}
